<?php
require_once $ubi['SERVER']['SERVER_ROOT'] . '/dao/dbConnection.php';
require_once $ubi['SERVER']['SERVER_ROOT'] . '/model/rating.php';

class RatingDao {
	private $_db;

	public function __construct() {
		$this->_db = new DbConnection();
		$this->_db->connect();
	}

	public function addRating($rating) {
		$query = sprintf("CALL sp_AddRating('%u', '%u', '%u', '%d', '%u')", 
			$rating->getUserId(), $rating->getInteractType(), $rating->getIndicteeId(),  
			$rating->getRatingValue(), $rating->getCreatedDate());
			
		$result = $this->_db->executeQuery($query);

		if (!$result) {
		    echo 'Could not run query: ' . ' ' . mysql_error();
		    return 0;
		}
		
		$row = mysql_affected_rows();		
		
		return ($row != 0);		
	}	
	
	public function isRating ($rating) {		
		$query = sprintf(
			"SELECT * FROM ratings WHERE UserId = '%u' AND InteractType = '%u' AND IndicteeId = '%u' AND RatingValue = '%d'", 
			$rating->getUserID(), $rating->getInteractType(), $rating->getIndicteeID(), $rating->getRatingValue());
		
		$result = $this->_db->executeQuery($query);

		if (!$result) {
			echo 'Could not run query: ' . ' ' . mysql_error();
		    return;
		}
		
		if (mysql_fetch_row($result)) {
			mysql_free_result($result);
			return true;
		}
		return false;
	}
	
	public function doRateItem($rating) {
		global $ubi;		
		
		$query = sprintf("CALL sp_RateItem('%u', '%u', '%u', '%d', '%u')", 
						$rating->getUserID(), $rating->getIndicteeID(), $rating->getInteractType(),
						$rating->getRatingValue(), $rating->getCreatedDate());
				
		$result = $this->_db->executeQuery($query);

		if (!$result) {
			echo 'Could not run query: ' . $query . ' ' . mysql_error();
			return;		
		}
		
		return 1;
	}
	
	public function doRateProvider($rating) {
		global $ubi;		
		
		$query = sprintf("CALL sp_RateProvider('%u', '%u', '%u', '%d', '%u')", 
						$rating->getUserID(), $rating->getIndicteeID(), $rating->getInteractType(),
						$rating->getRatingValue(), $rating->getCreatedDate());		
		
		$result = $this->_db->executeQuery($query);

		if (!$result) {
			echo 'Could not run query: ' . $query . ' ' . mysql_error();
			return;		
		}
		
		return 1;
	}
	
	public function doRateComment($rating) {
		global $ubi;		
		
		$query = sprintf("CALL sp_RateComment('%u', '%u', '%u', '%d', '%u')", 
						$rating->getUserID(), $rating->getIndicteeID(), $rating->getInteractType(),
						$rating->getRatingValue(), $rating->getCreatedDate());		
		
		$result = $this->_db->executeQuery($query);

		if (!$result) {
			echo 'Could not run query: ' . $query . ' ' . mysql_error();
			return;		
		}
		
		return 1;
	}

	public function getRating($userID, $indicteeID, $interactType) {
		$query = sprintf("SELECT * FROM ratings WHERE UserId = '%u' AND IndicteeId = '%u' AND InteractType = '%u'",
					$userID, $indicteeID, $interactType);
		$result = $this->_db->executeQuery($query);					
		if (!$result) {
			echo 'Could not run query: ' . $query . ' ' . mysql_error();
			return;		
		}
		
		if ($row = mysql_fetch_assoc($result)) {			
			$rating = new RatingModel();
			$rating->setCreatedDate($row['CreatedDate']);
			$rating->setIndicteeID($indicteeID);
			$rating->setInteractType($interactType);
			$rating->setRatingValue($row['RatingValue']);
			$rating->setUserID($userID);

			return $rating;
		}
		
		mysql_free_result($result);
	}
}
?>